#pragma once

#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
#include "rtda/heap/class_loader.h"
#include "rtda/heap/object.h"

namespace jvm {
    namespace heap {

        class StringPool {
        private:
            static StringPool instance;
            std::unordered_map<std::string, ObjectPtr> internedStrings;

            // Private constructor
            StringPool() = default;

            // Delete copy constructor and assignment operator
            StringPool(const StringPool&) = delete;
            StringPool& operator=(const StringPool&) = delete;

            // utf8 -> utf16
            std::vector<uint16_t> stringToUtf16(const std::string& s);

            // utf16 -> utf8
            std::string utf16ToString(const std::vector<uint16_t>& s);

        public:
            // Get the singleton instance
            static StringPool& getInstance();

            // cpp string(utf8) -> java.lang.String
            ObjectPtr JString(std::shared_ptr<ClassLoader> loader, const std::string& goStr);

            // java.lang.String -> CPP string(utf8)
            std::string CPPString(ObjectPtr jStr);

            ObjectPtr internString(ObjectPtr jStr);
        };

    } // namespace heap
}// namespace jvm